package com.mti.monitor.util;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

@Slf4j
public class LicenseUtils {
    private static final int SPLIT_LENGTH = 4;
    private static final int SPLIT_TOTAL = 4 * SPLIT_LENGTH;
    private static final String SPLIT = "-";

    public static String getMachineCode(String unique) {
        StringBuffer sb = new StringBuffer();
        sb.append("unique: ").append(unique);
        String code = checkSum(sb.toString());
        return getSplitString(code, SPLIT, SPLIT_LENGTH);
    }

    private static String checkSum(String input) {
        Long h = 0l;
        for (char item : input.toCharArray()) {
            h = (h << 5) - h + item;
        }
        String code = String.format("%X", h);
        while (code.length() < SPLIT_TOTAL) {
            code = "0" + code;
        }
        return code;
    }

    private static String getSplitString(String str, String split, int length) {
        int len = str.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            if (i % length == 0) {
                sb.append(split);
            }
            sb.append(str.charAt(i));
        }
        String[] attrArr = sb.toString().split(split);
        sb = new StringBuffer();
        for (String attr : attrArr) {
            if (attr.length() == length) {
                sb.append(attr).append(split);
            }
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

    public static boolean checkLicence(String pk, String sn, String mc) {
        mc = mc.replace("-", "");
        String body = sn.substring(0, 16);
        String sign = sn.substring(16);
        return true;//RSAUtils.verify(Base64.getEncoder().encodeToString((body + mc).getBytes()), pk, sign);
    }

    public static Long getExpiredDate(String pk, String sn, String mc) {
        if (!checkLicence(pk, sn, mc)) {
            return null;
        }
        String body = sn.substring(0, 16);
        return Long.valueOf(body) / 1000;
    }

    public static Integer getLeftDay(Long expiredDate) {
        long ms = expiredDate - new Date ().getTime ();

        int day = (int) ( ms / 1000 / 60 / 60 / 24 );

        return day;
    }

    public static String getLeftTime(Long expiredDate) {
        long ms = expiredDate - new Date ().getTime ();

        int day = (int) ( ms / 1000 / 60 / 60 / 24 );

        int hour = (int) ( ms / 1000 / 60 / 60 % 24 );

        int minute = (int) ( ms / 1000 / 60 % 60 );

        int second = (int) ( ms / 1000 % 60 );

        return (day + " 天 " + hour + " 时 " + minute + " 分 " + second + " 秒");
    }


    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
}
